#!/bin/bash

# mplayer-lastfm (mkalog) is part of http://code.google.com/p/camtools,
# Copyright 2009 Camille Huot,
# and is licensed under http://www.gnu.org/copyleft/gpl.html

# Mkalog is a shell script which can send to last.fm infos about a mka file
# you listened in the past, by specifying the date.

# WARNING: to use it, you need lastfmsubmitd
# * lastfmsubmitd
#   Please see http://www.red-bean.com/~decklin/software/lastfmsubmitd/
#   and instructions inside my lmplayer (from the mplayer-lastfm distribution).

# More information about Matroska, chapters and tags are available here:
# http://www.cameuh.net/pmwiki.php/Docs/MatroskaAudio


# checking tools
which mkvextract &> /dev/null || {
 echo "mkalog needs mkvextract"
 exit
}
which xsltproc &> /dev/null || {
 echo "mkalog needs xsltproc"
 exit
}

# creating workdir
mydir="/tmp/mkalog-$$"
rm -f "$mydir" || {
 echo "Unable to delete old tempfiles in $mydir"
 exit
}
mkdir -p "$mydir" || {
 echo "Unable to create the workdir $mydir"
 exit
}

# checking parameters
[ $# -gt 1 ] || {
 echo "Bad parameters. Correct Syntax is:"
 echo "mkalog <the gnu date when you listened your music> <file.mka> ..."
 echo "Example: mkalog \"yesterday 10pm\" Toto-live.mka Bon_Jovi.mka"
 exit
}

# checking the supplied date
start_date="$1"
date --date "$start_date" &> /dev/null || {
 echo "Bad date format, please read man date."
 exit
}
shift

cat > "$mydir"/chapters.xsl <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:for-each select="Chapters/EditionEntry/ChapterAtom">
<xsl:value-of select="ChapterUID"/>
<xsl:text> </xsl:text>
<xsl:value-of select="ChapterTimeStart"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
EOF
cat > "$mydir"/tags-album.xsl <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:for-each select="Tags/Tag">
<xsl:if test='Targets/TargetType="ALBUM"'>
<xsl:for-each select="Simple">
<xsl:choose>
<xsl:when test='Name="TITLE"'>
<xsl:text>ALBUM </xsl:text>
<xsl:value-of select="String"/>
<xsl:text>
</xsl:text>
</xsl:when>
<xsl:when test='Name="ARTIST"'>
<xsl:text>ARTIST </xsl:text>
<xsl:value-of select="String"/>
<xsl:text>
</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
EOF
cat > "$mydir"/tags-tracks.xsl <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:for-each select="Tags/Tag">
<xsl:if test='Targets/TargetType="TRACK"'>
<xsl:value-of select="Targets/ChapterUID"/>
<xsl:text> </xsl:text>
<xsl:for-each select="Simple">
<xsl:if test='Name="TITLE"'>
<xsl:value-of select="String"/>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
EOF

str() {
 len=13
 txt="$*"
 printf "%-${len}s" "$(eval echo "\${txt:0:$len}")"
}

nb=0 # number of the current file
while [ $# -gt 0 ]; do
 file="$1"
 fname=$(basename $file)
 echo "Processing: \"${fname}\""
 echo "Extracting chapters"
 mkvextract chapters "${file}" > "${mydir}/${fname}-chapters.xml"
 echo "Reading chapters"
 xsltproc --novalid "$mydir"/chapters.xsl "${mydir}/${fname}-chapters.xml" \
  | grep -v '^$' | sed '1d;s/\..*$//' > "${mydir}/${fname}-chapters.txt"
 echo "Extracting tags"
 mkvextract tags "${file}" > "${mydir}/${fname}-tags.xml"
 echo "Reading global tags"
 xsltproc --novalid "$mydir"/tags-album.xsl "${mydir}/${fname}-tags.xml" \
  | grep -v '^$' | sed "1d" > "${mydir}/${fname}-tags-album.txt"
 echo "Reading track tags"
 xsltproc --novalid "$mydir"/tags-tracks.xsl "${mydir}/${fname}-tags.xml" \
  | grep -v '^$' | sed "1d" > "${mydir}/${fname}-tags-tracks.txt"

 line=$(grep "^ARTIST " "${mydir}/${fname}-tags-album.txt")
 artist=${line#ARTIST }
 line=$(grep "^ALBUM " "${mydir}/${fname}-tags-album.txt")
 album=${line#ALBUM }
 cat "${mydir}/${fname}-chapters.txt" | while read line; do
  chapuid=${line%% *}
  offset=${line#* }
  line=$(grep ${chapuid} "${mydir}/${fname}-tags-tracks.txt")
  title=${line#* }

  hours=$(echo ${offset} | cut -d: -f1)
  minutes=$(echo ${offset} | cut -d: -f2)
  seconds=$(echo ${offset} | cut -d: -f3)
  now="${start_date} + $(($nb * 75)) minutes"
  now="${now} + ${hours} hours + ${minutes} minutes + ${seconds} seconds"
  now=$(date --date "$now" "+%Y-%m-%d %H:%M:%S")

  echo -n "Sending to Lastfm: "
  echo "$(str ${artist})-$(str ${album})-$(str ${title})-${now}"
  lastfmsubmit --artist "${artist}" --title "${title}" --length 60 \
   --time "${now}" --album "${album}"
 done
 nb=$(($nb+1))
 shift
done
echo "Success"
rm -rf "$mydir"
